package br.pucrio.inf.aaaong.models.domain.database.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.GregorianCalendar;
import java.util.List;

import br.com.caelum.vraptor.ioc.Component;
import br.pucrio.inf.aaaong.models.database.exceptions.PostgreSQLConstraintException;
import br.pucrio.inf.aaaong.models.database.update.UpdateQueryExecutor;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.AdolescenteDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.ArtificialIdDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.CandidatoDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.CelularDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.EscolaridadeDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.PreInscritoDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.TelefoneFixoDAO;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.beans.AdolescenteBean;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.beans.CelularBean;
import br.pucrio.inf.aaaong.models.domain.database.dao.basic.beans.TelefoneFixoBean;
import br.pucrio.inf.aaaong.models.domain.database.dao.beans.PreInscricaoBeans;
import br.pucrio.inf.aaaong.models.domain.form.beans.PreInscricaoFormBean;

@Component
public class PreInscricaoDAO {
	private Connection connection;
	private AdolescenteDAO adolescenteDao;
	private ArtificialIdDAO artificialIdDao;
	private CandidatoDAO candidatoDao;
	private PreInscritoDAO preInscritoDao;
	private TelefoneFixoDAO telefoneFixoDao;
	private CelularDAO celularDao;
	private EscolaridadeDAO escolaridadeDao;

	public PreInscricaoDAO(Connection connection) {
		UpdateQueryExecutor queryExecutor = new UpdateQueryExecutor(connection);
		this.artificialIdDao = new ArtificialIdDAO(connection);
		this.adolescenteDao = new AdolescenteDAO(queryExecutor);
		this.candidatoDao = new CandidatoDAO(queryExecutor);
		this.preInscritoDao = new PreInscritoDAO(queryExecutor);
		this.telefoneFixoDao = new TelefoneFixoDAO(queryExecutor);
		this.celularDao = new CelularDAO(queryExecutor);
		this.escolaridadeDao = new EscolaridadeDAO(queryExecutor);
		this.connection = connection;
	}
	
	public void insert(PreInscricaoBeans beans) throws PostgreSQLConstraintException {
		int id = this.artificialIdDao.nextFor(ArtificialIdDAO.Type.Adolescente);
		beans.setId(id);
		adolescenteDao.insert(beans.getAdolescente());
		candidatoDao.insert(beans.getCandidato());
		preInscritoDao.insert(beans.getPreInscrito());
		
		telefoneFixoDao.deleteAllOfAdolescente(id);
		List<TelefoneFixoBean> telefones = beans.getTelefonesFixos();
		for (TelefoneFixoBean telefone : telefones) {
			telefoneFixoDao.insert(telefone);
		}
		
		celularDao.deleteAllOfAdolescente(id);
		List<CelularBean> celulares = beans.getCelulares();
		for (CelularBean celular : celulares) {
			celularDao.insert(celular);
		}
		
		escolaridadeDao.insert(beans.getEscolaridade());
		
		try {
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("Commit failed");
		}
	}
}
